package net.xju.dao;

import net.xju.common.utils.JdbcUtilsV1;
import net.xju.common.utils.PageInfo;
import net.xju.entity.Goods;
import net.xju.entity.Vo.GoodsVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GoodsDao {
    public PageInfo<GoodsVo> pageList(Integer pageNo, Integer pageSize, Goods goods) {

        String sql = "SELECT g.id,g.`name`,g.intro,g.flag,g.picture,g.price,g.rating,g.pubdate,g.type_id typeId,g.create_time createTime,gt.`name` typeName FROM goods g LEFT JOIN goodstype gt ON g.type_id = gt.id WHERE g.deleted = 0 ";
        List<Object> params = new ArrayList<Object>();
        // 拼接sql
        if(goods.getName()!=null&&!goods.getName().isEmpty()){
            sql += " AND g.`name` LIKE ? ";
            params.add("%"+goods.getName()+"%");
        }
        if (goods.getPubdate() != null) {
            sql += " AND g.`pubdate` = ? ";
            params.add(goods.getPubdate());
        }
        if (goods.getTypeId() != null) {
            sql += " AND g.`type_id` = ? ";
            params.add(goods.getTypeId());
        }
        if(goods.getFlag()!=null){
            sql += " AND g.`flag` = ? ";
            params.add(goods.getFlag());
        }
        sql += "limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<GoodsVo> goodsVos = JdbcUtilsV1.executeQuery(GoodsVo.class, sql, params.toArray());
        String countSql = "SELECT COUNT(*) FROM goods WHERE deleted = 0 ";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);
        return new PageInfo<>(pageNo,pageSize,(int)count,goodsVos);
    }

    public boolean insert(Goods goods) {
        String sql = "insert into goods(name,pubdate,flag,type_id,intro,picture,price,rating,create_time) values(?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(goods.getName());
        params.add(goods.getPubdate());
        params.add(goods.getFlag());
        params.add(goods.getTypeId());
        params.add(goods.getIntro());
        params.add(goods.getPicture());
        params.add(goods.getPrice());
        params.add(goods.getRating());
        params.add(goods.getCreateTime());
        int i = JdbcUtilsV1.executeUpdate(sql, params.toArray());
        return i>0;
    }

    public boolean delete(int id) {
        String sql = "update goods set deleted = 1 where id = ?";
        int i = JdbcUtilsV1.executeUpdate(sql, id);
        return i>0;
    }

    public boolean update(Goods goods) {
        StringBuilder sql = new StringBuilder("update goods set ");
        List<Object> params = new ArrayList<>();
        if (goods.getRating() != null) {
            sql.append("`rating` = ?, ");
            params.add(goods.getRating());
        }

        if(goods.getName()!=null&&!goods.getName().isEmpty()){
            sql.append("`name` = ?, ");
            params.add(goods.getName());
        }
        if(goods.getPubdate()!=null){
            sql.append("`pubdate` = ?, ");
            params.add(goods.getPubdate());
        }
        if(goods.getTypeId()!=null){
            sql.append("`type_id` = ?, ");
            params.add(goods.getTypeId());
        }
        if(goods.getFlag()!=null){
            sql.append("`flag` = ?, ");
            params.add(goods.getFlag());
        }
        if(goods.getIntro()!=null&&!goods.getIntro().isEmpty()){
            sql.append("`intro` = ?, ");
            params.add(goods.getIntro());
        }
        if(goods.getPicture()!=null&&!goods.getPicture().isEmpty()){
            sql.append("`picture` = ?, ");
            params.add(goods.getPicture());
        }
        if(goods.getPrice()!=null){
            sql.append("`price` = ?, ");
            params.add(goods.getPrice());
        }
        if(!params.isEmpty()){
            sql.deleteCharAt(sql.lastIndexOf(","));
        }
        sql.append(" WHERE id = ?");
        params.add(goods.getId());
        int i = JdbcUtilsV1.executeUpdate(sql.toString(), params.toArray());
        return i>0;
    }
}
